/**
 * Created by kitabi on 2017/6/20.
 */
/**
 * Created by kitabi on 2017/6/16.
 */
var classify={

};

var $btnStaffConfirm= $("#btnStaffConfirm");
var $staffDialog= $("#staffDialog");
/**
 * 获取用户数据
 */
classify.queryList= function(){


    $("#mainTable").bootstrapTable({

        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        striped: true,                              //是否显示行间隔色
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        url:"/order/qryOrderList",
        // height: $(window).height() - 110,
        // width:$(window).width(),
        // showColumns:true,
        pagination:true,
        // queryParams : queryParams,
        minimumCountColumns:2,
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 10,                       //每页的记录行数（*）
        pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
        uniqueId: "id",                     //每一行的唯一标识，一般为主键列
        showExport: true,
        exportDataType: 'all',


        paginationPreText:"<",
        paginationNextText:">",
        queryParams: function(params) {

            debugger;
            var userSearch= $("#userSearch").val();

            var userDateStart=$("#userDateStart").val();
            var userDateEnd=$("#userDateEnd").val();
            return {
                cPage: (params.offset/params.limit)+1,
                pSize: params.limit,
                classify:$("#classifylist").val(),
                status:$("#userStatus").val(),
                offerName:userSearch,
                start:userDateStart,
                end:userDateEnd,
                city:$("#city").val(),
                province:$("#province").val(),
                area:$("#area").val()
            };
        },
        columns: [
            {
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            },{
                field: 'order_nbr',
                title: '订单号'
            },{
                field: 'offer_name',
                title: '商品名称'
            },{
                field: 'total_fee',
                title: '商品价格',
                formatter: function(value,row,index){


                    return parseFloat(value/100);
                }
            },{
                field: 'nick_name',
                title: '用户姓名'
            },{
                field: 'phone',
                title: '电话'
            },{
                field: 'create_time',
                title: '下单时间'
            }, {

                field: 'status',
                title: '状态',
                align: 'center',

                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var status= row.status;
                    var name="";
                    if(status ==0){
                        name="已取消";
                    }else if(status ==1){
                        name="待支付";
                    }else if(status ==2){
                        name="待发货";
                    }else if(status ==3){
                        name="待收货";
                    }else if(status ==4){
                        name="待评价";
                    }else if(status ==5){
                        name="已完成";
                    }else if(status ==6){
                        name="退款中";
                    }else if(status ==7){
                        name="已退款";
                    }else if(status ==-1){
                        name="关闭";
                    }
                    return  name;
                }
            }, {

                field: 'status',
                title: '操作',


                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var id= row.id;
                    var status= row.status;

                    var add="";

                    var tuikuan="";
                    if(status==2){
                        add='<button type="button" onclick="fahuo('+id+')" style="margin-right: 5px;" class="btn btn-sm btn-primary btn-trans">发货</button>';
                        tuikuan='<button type="button" onclick="updateStatus('+id+',\'7\')" style="margin-right: 5px;" class="btn btn-sm btn-danger btn-trans">退款</button>';
                    }else if(status==6 ){
                        tuikuan='<button type="button" onclick="updateStatus('+id+',\'7\')" style="margin-right: 5px;" class="btn btn-sm btn-danger btn-trans">退款</button>';
                    }
                    var e='<button type="button" onclick="showDetail('+id+')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">编辑</button>';
                    var d ="";
                    if(status!=-1 && status!=2 && status!=3 && status!=5 && status !=0) {
                         d = '<button type="button" onclick="updateStatus(' + id + ',\'-1\')" style="margin-right: 5px;" class="btn btn-sm btn-danger btn-trans">关闭</button>';
                    }
                    return  add+tuikuan+e+d;
                }
            }]

    });

}

function fahuo(id){//,\'3\'


    $("#express_id").val("");
    $("#express_num").val("");

    $staffDialog.attr("order_id",id);

    $staffDialog.show();

}


function updateStatus(id ,type){

    if (type == '7'){
        if(confirm("确定要执行退款操作吗？支付宝跟微信支付的订单将原路返回给用户！")){

            var param={
                id: id,
                status: type
            }
            Ajax.ajax({
                url: "/order/updateStatus",
                async: false,
                data: param,
                type: "POST",
                dataType: 'json',
                success: function (res) {
                    if (res.resultCode == 0) {
                        $("#mainTable").bootstrapTable('refresh');
                    } else {
                        alert("操作失败");
                    }
                },
                finally: function(){
                    $("#mainTable").bootstrapTable('refresh');
                }
            });
        }
    }else if(type== '-1'){
        if(confirm("确定要执行关闭订单操作吗？")) {
            var param = {
                id: id,
                status: type
            }
            Ajax.ajax({
                url: "/order/updateStatus",
                data: param,
                type: "POST",
                dataType: 'json',
                success: function (res) {
                    if (res.resultCode == 0) {
                        $("#mainTable").bootstrapTable('refresh');
                    } else {
                        alert("操作失败");
                    }
                },
                error: function () {
                    alert("操作失败");
                }
            });
        }
    }else{



        var param={
            id: id,
            status: type
        }
        Ajax.ajax({
            url: "/order/updateStatus",
            data: param,
            type: "POST",
            dataType: 'json',
            success: function (res) {
                if (res.resultCode == 0) {
                    $("#mainTable").bootstrapTable('refresh');
                } else {
                    alert("操作失败");
                }
            },
            error: function(){
                alert("操作失败");
            }
        });
    }
}


function allpush(status){

    var select= $("#mainTable").bootstrapTable('getSelections');

    if(select.length ==0){
        alert("请先勾选一行");
        return;
    }

    var idlist= "";
    for(var i=0;i<select.length;i++){
        var temp = select[i];
        if(status ==3){
            if(temp.status != 2){

                alert("请注意，提现单号: "+temp.id+"  已发货或还未支付！");
                return ;
            }
        }

        if(status ==7){
            if(temp.status != 6 ){

                alert("请注意，提现单号: "+temp.id+"  不能退款！");
                return ;
            }
        }

        if(i==0){
            idlist+=temp.id;
        }else {
            idlist += "," + temp.id;
        }
    }

    var param={
        id: idlist,
        status: status

    }
    Ajax.ajax({
        url: "/order/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function addType() {
    var brandName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(brandName == ""){
        alert("请输入品牌名称");
        return;
    }
    var param = {
        brandName:brandName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/add",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function clearDialog() {
    $("#Sname").val("");
    $("#indexs").attr("data-id","");
    $("#indexs").val("");
    $("#userSearch").val();
    $staffDialog.hide();
}

function showDetail(index){

    $('#main-content').load('/order/getOrderDetail?id='+index);

}
function updateType() {
    var id = $("#indexs").attr('data-id');
    var offerTypeName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(offerTypeName == ""){
        alert("请输入名称");
        return;
    }
    var param = {
        id:id,
        offerTypeName:offerTypeName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/update",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function queryProvince(){
    var param = {

    }
    Ajax.ajax({
        url: "/common/qryProvinces",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option value='"+res[i].provinceid+"'>"+res[i].province+"</option>");
                $("#province").append(option);
            }

        }
    });
}

function queryCitys(){

    var pro = $("#province").val();
    if(pro == ""){
        $("#city").find("option[add]").remove();
        $("#area").find("option[add]").remove();
        return;
    }

    var param = {
        province:$("#province").val()
    }
    Ajax.ajax({
        url: "/common/qryCities",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option add='add' value='"+res[i].cityid+"'>"+res[i].city+"</option>");
                $("#city").append(option);
            }


        }
    });
}

function qryAreas(){

    var pro = $("#city").val();
    if(pro == ""){
        $("#area").find("option[add]").remove();
        return;
    }

    var param = {
        city:$("#city").val()
    }
    Ajax.ajax({
        url: "/common/qryAreas",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option add='add'  value='"+res[i].areaid+"'>"+res[i].area+"</option>");
                $("#area").append(option);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function saveExpress(){
    var express_id = $("#express_id").val();
    var express_num = $("#express_num").val();
    debugger;
    if(express_id==""){
        alert("请选择快递");
        return;
    }
    if(express_num ==""){
        alert("请输入快递编号");
        return;
    }
    debugger;
    var param = {
        id:$staffDialog.attr("order_id"),
        express_id:express_id,
        express_num:express_num
    }
    Ajax.ajax({
        url: "/order/saveExpress",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            debugger;
            alert(res.data);
            $staffDialog.hide();
            $("#mainTable").bootstrapTable('refresh');
        },
        error: function(){
            alert("操作失败");
        }
    });
}

$(function(){
    //开始\结束 日期选择
    $("#userDateStart").datepicker({
        format: 'yyyy-mm-dd ',  language: 'zh-CN',                 autoclose: true,                 todayHighlight: true
    });
    $("#userDateEnd").datepicker({
        format: 'yyyy-mm-dd ',  language: 'zh-CN',                 autoclose: true,                 todayHighlight: true
    });
    //关闭窗口
    $('#staffDialogClose').on('click',function(){

        $staffDialog.hide();
    });

    classify.queryList();

    queryProvince();
    Ajax.ajax({
        url: "/order/queryExpress",

        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option value='"+res[i].id+"'>"+res[i].name+"</option>");
                $("#express_id").append(option);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });


    $("#userSearchBtn").click(function(){
        $("#mainTable").bootstrapTable('refresh');
    });





});